import * as systemService from '../services/system';
import { routerRedux } from 'dva/router';
import { message } from 'antd';

export default {
  namespace: 'mflogin',
  state:{
  },
  reducers: {
  },
  effects: {
    *dologin({payload},{call,put}){
      window.sessionStorage.removeItem("accessToken");
      window.sessionStorage.removeItem("logined");
      window.sessionStorage.removeItem("resources");
      window.sessionStorage.removeItem("loginName");
      window.sessionStorage.removeItem("loginInfo");

      const {data} = yield call(systemService.login, payload);
      if(data==null){
        return;
      }
      window.sessionStorage.setItem("accessToken",JSON.stringify(data.accessToken));
      window.sessionStorage.setItem("logined",true);
      window.sessionStorage.setItem("loginName",JSON.stringify(data.name));
      window.sessionStorage.setItem("loginInfo",JSON.stringify(data));
      yield put({ type: 'main/getResource', payload: null });
    },
    *dologinout({payload},{call,put}){
      // yield put(routerRedux.push('/login'));
      yield call(systemService.loginout, payload);
      window.sessionStorage.removeItem("accessToken");
      window.sessionStorage.removeItem("logined");
      window.sessionStorage.removeItem("resources");
      window.sessionStorage.removeItem("loginName");
      window.sessionStorage.removeItem("locationPathname");
      window.sessionStorage.removeItem("selectedKeyPayload");
      window.sessionStorage.removeItem("loginInfo");
      window.location.href='';
    },
    *resetPswd({payload},{call,put}){
     const {data} = yield call(systemService.resetPswd, payload);
     if(data==='ok'){
       // yield put(routerRedux.push('/login'));
       message.success("密码修改成功",3);
       window.sessionStorage.removeItem("accessToken");
       window.sessionStorage.removeItem("logined");
       window.sessionStorage.removeItem("resources");
       window.sessionStorage.removeItem("loginName");
       window.sessionStorage.removeItem("loginInfo");
       window.location.href='';
     }
    },

  },
  subscriptions: {
  },

}
